home *** CD-ROM | disk | FTP | other *** search
/ CDUTIL 13 / CDUTIL #13 Julio 1995.iso / windows / acadcom / ads / sample / dlgtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-08  |  14.7 KB  |  579 lines

  1. /* Next available MSG number is  21 */
  2. /*
  3.            ADS Programmable Dialog Box Test Program 
  4.  
  5.    DLGTEST.C
  6.  
  7.    Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994 by Autodesk, Inc.
  8.  
  9.    Permission to use, copy, modify, and distribute this software in 
  10.    object code form for any purpose and without fee is hereby granted, 
  11.    provided that the above copyright notice appears in all copies and 
  12.    that both that copyright notice and the limited warranty and 
  13.    restricted rights notice below appear in all supporting 
  14.    documentation.
  15.  
  16.    AUTODESK PROVIDES THIS PROGRAM "AS IS" AND WITH ALL FAULTS.  
  17.    AUTODESK SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTY OF 
  18.    MERCHANTABILITY OR FITNESS FOR A PARTICULAR USE.  AUTODESK, INC.
  19.    DOES NOT WARRANT THAT THE OPERATION OF THE PROGRAM WILL BE 
  20.    UNINTERRUPTED OR ERROR FREE.
  21.  
  22.    Use, duplication, or disclosure by the U.S. Government is subject to 
  23.    restrictions set forth in FAR 52.227-19 (Commercial Computer 
  24.    Software - Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) 
  25.    (Rights in Technical Data and Computer Software), as applicable.
  26.     
  27.    .
  28.  
  29.     Programmable Dialog Box Test Program
  30.  
  31.      This program is the ADS counterpart to the LISP test
  32.     program, dlgtest.lsp.  It provides a simple dimensioning
  33.     dialog invoked with the command "dimen" and a simple color
  34.     dialog invoked with the command "setcolor".
  35.  
  36.      The purposes of providing this program:
  37.     1) Demonstrate Programmable Dialog Box use with minimum of code
  38.     to sort through
  39.     2) Demonstrate differences between LISP and ADS dialog 
  40.     programming
  41.     3) Use as a starting point to try out new dialog code
  42.  
  43.      Dlgtest uses the file dlgtest.dcl as the DCL (Dialog 
  44.     Control Language) file.
  45.  
  46.      ADS functions are associated with dialog buttons with 
  47.     the ads_action_tile functions.  These functions are called 
  48.     when the user presses buttons during the ads_start_dialog 
  49.     function.
  50.  
  51.     Special tile names (keys): 
  52.         "accept" - Ok button
  53.         "cancel" - Cancel button
  54.  
  55. */
  56.  
  57.  
  58. #include <stdio.h>
  59. #include <string.h>
  60.  
  61. #include "adslib.h"
  62. #include "adsdlg.h"
  63.  
  64.  
  65. /* All Function Prototypes for DLGTEST.C */
  66.  
  67. void main _((int argc, char *argv[]));
  68. int loadfuncs _((void));
  69. int dofun _((void));
  70. void setdimen _((void));
  71. void get_dimvars _((ads_hdlg hdlg));
  72. void set_dimvars _((ads_hdlg hdlg));
  73. void CALLB dimen_ok   _((ads_callback_packet *cpkt));
  74. void CALLB editcol_cb _((ads_callback_packet *cpkt));
  75. void CALLB listcol_cb _((ads_callback_packet *cpkt));
  76. void CALLB help_cb    _((ads_callback_packet *cpkt));
  77. int setcolor _((void));
  78.  
  79. #ifdef __STDC__
  80. /* With an old style definition, the short code parameter is promoted to an
  81.    int, and so a short parameter should be prototyped as an int.  This is
  82.    ANSI C. */
  83. void dlg_colortile _((ads_hdlg hdlg, char *key, int color, int borderflag));
  84. void dlg_rect _((int x, int y, int width, int height, int color));
  85. #else
  86. void dlg_colortile _((ads_hdlg hdlg, char *key, short color, int borderflag));
  87. void dlg_rect _((short x, short y, short width, short height, short color));
  88. #endif
  89.  
  90.  
  91. /* Subroutines names to be registered with AutoLisp and provided in this
  92.    application. */
  93.  
  94. static char *exfun[] = {/*MSG0*/"C:dimen", /*MSG0*/"C:setcolor", NULL};
  95.  
  96.  
  97. static char *dimbools[] = {
  98.     /*MSG0*/"dimse1", 
  99.     /*MSG0*/"dimse2", 
  100.     /*MSG0*/"dimtih", 
  101.     /*MSG0*/"dimtoh",
  102.     /*MSG0*/"dimtad",
  103.     /*MSG0*/"dimtol", 
  104.     /*MSG0*/"dimlim", 
  105.     /*MSG0*/"dimalt", 
  106.     /*MSG0*/"dimaso",
  107.     /*MSG0*/"dimsho",
  108.     NULL
  109.    };
  110.  
  111. static char *dimreals[] = {
  112.     /*MSG0*/"dimasz", 
  113.     /*MSG0*/"dimtsz", 
  114.     /*MSG0*/"dimtxt", 
  115.     /*MSG0*/"dimcen",
  116.     /*MSG0*/"dimexo",
  117.     /*MSG0*/"dimexe", 
  118.     /*MSG0*/"dimdle", 
  119.     NULL
  120.    };
  121.  
  122. #ifndef BLACK
  123. #define    BLACK    0
  124. #define    RED        1
  125. #define    YELLOW   2
  126. #define    GREEN    3
  127. #define    CYAN     4
  128. #define    BLUE     5
  129. #define    MAGENTA  6
  130. #define    WHITE    7
  131. #endif
  132.  
  133. static short color;
  134. #define CLEN 32
  135. static char colorstr[CLEN+1];
  136.  
  137. static char *colorlist[] = {
  138.     /*MSG1*/"black", 
  139.     /*MSG2*/"red", 
  140.     /*MSG3*/"yellow", 
  141.     /*MSG4*/"green", 
  142.     /*MSG5*/"cyan", 
  143.     /*MSG6*/"blue",
  144.     /*MSG7*/"magenta", 
  145.     /*MSG8*/"white"
  146.    };
  147.  
  148.  
  149.  
  150. /* MAIN -- the main routine */
  151. void
  152. /*FCN*/main(argc,argv)
  153.   int argc; char *argv[];
  154. {
  155.     short scode = RSRSLT;
  156.     int stat;
  157.     char errmsg[80];
  158.  
  159.     ads_init(argc, argv);
  160.  
  161.     for ( ;; ) {
  162.  
  163.     if ((stat = ads_link(scode)) < 0) {
  164.         sprintf(errmsg,
  165.                     /*MSG9*/"DLGTEST: bad status from ads_link() = %d\n",
  166.             stat);
  167. #ifdef Macintosh
  168.         macalert(errmsg);
  169. #else
  170.         puts(errmsg);
  171.         fflush(stdout);
  172. #endif /* Macintosh */
  173.         exit(1);
  174.     }
  175.  
  176.     scode = RSRSLT;           /* default return code */
  177.  
  178.     /* Check for AT LEAST the following cases here */
  179.     switch (stat) {
  180.  
  181.         /* Load or define Lisp functions */
  182.     case RQXLOAD:              /* Load request.    Send function defuns */
  183.         scode = loadfuncs() ? -RSRSLT : -RSERR;
  184.         break;
  185.  
  186.         /* Unload or undefine ALL functions previously defined. */
  187.     case RQXUNLD:              /* Unload request. Do cleanup */
  188.         /* This defaults to sending an RSRSLT.  If you send an 
  189.            RSERR, you can refuse to unload the program, but Lisp
  190.            will still ask you to terminate. */
  191.         break;
  192.  
  193.             /* Execute a "loaded" function that was defined via RQXLOAD */
  194.     case RQSUBR:
  195.         dofun();
  196.         break;
  197.  
  198.     default:
  199.         break;
  200.     }
  201.     }
  202. }
  203.  
  204.  
  205.  
  206. /* LOADFUNCS  --  Define external functions with AutoLISP */
  207. static int
  208. /*FCN*/loadfuncs()
  209. {
  210.     int i;
  211.  
  212.     for (i = 0; exfun[i] != NULL; i++) {
  213.     if (!ads_defun(exfun[i], i))
  214.         return FALSE;
  215.     }
  216.     ads_printf(/*MSG10*/"Functions: 1-dimen 2-setcolor.\n");
  217.     return TRUE;
  218.  
  219. }
  220.  
  221. /* Execute a defined function. */
  222.  
  223. static int
  224. /*FCN*/dofun()
  225. {
  226.     int id;
  227.     struct resbuf *rb;
  228.  
  229.     /* Get the function code from the combuf */
  230.     if ((id = ads_getfuncode()) < 0)
  231.     return 0;
  232.  
  233.     if ((rb = ads_getargs()) != NULL) {
  234.         ads_printf("No arguments expected");
  235.     ads_relrb(rb);
  236.     }
  237.  
  238.     switch (id) {              /* Which function is called? */
  239.  
  240.     case 0:
  241.     /* dimen -- AutoCAD dimensioning dialog */
  242.     setdimen();
  243.     break;
  244.  
  245.     case 1:
  246.     /* setcolor -- AutoCAD color dialog */
  247.     setcolor();
  248.     break;
  249.  
  250.     default:
  251.     break;
  252.     }
  253.     return 1;
  254. }
  255.  
  256.  
  257. /* 
  258.  
  259.         DIMEN -- Dimensioning Dialog
  260.  
  261. */
  262.  
  263. /* Position of dialog, centered to start */
  264. static int dimx = -1, 
  265.        dimy = -1; 
  266.  
  267. void
  268. /*FCN*/setdimen()
  269. {
  270.     ads_hdlg hdlg;
  271.     int dlg_status;
  272.     int dcl_id;
  273.  
  274.     ads_load_dialog(/*MSG0*/"dlgtest.dcl", &dcl_id);
  275.     if (dcl_id < 0) {
  276.         ads_printf(/*MSG13*/"Error loading \"dlgtest.dcl\"\n");
  277.     return;
  278.     }
  279.     /* Display the "dimensions" dialog */
  280.     ads_new_positioned_dialog(/*MSG14*/"dimensions", dcl_id, 
  281.             (CLIENTFUNC)0, dimx, dimy, &hdlg);
  282.     if (hdlg == NULL) {
  283.         ads_printf(/*MSG15*/"The ads_new_dialog function failed\n");
  284.     ads_unload_dialog(dcl_id);
  285.     return;
  286.     }
  287.  
  288.     /* Register dimen_ok function with the OK button */
  289.     ads_action_tile(hdlg, /*MSG0*/"accept", (CLIENTFUNC)dimen_ok);
  290.  
  291.     /* show current values in dialog */
  292.     get_dimvars(hdlg);
  293.  
  294.     /* run the dialog */
  295.     ads_start_dialog(hdlg, &dlg_status);
  296.  
  297.     /* free all memory for dialog */
  298.     ads_unload_dialog(dcl_id);
  299. }
  300.  
  301.  
  302. /* DIMEN_OK -- callback function for OK button of dimension dialog.  */
  303.  
  304. static void CALLB
  305. /*FCN*/dimen_ok(cpkt)
  306.   ads_callback_packet *cpkt;
  307. {
  308.     /* User pressed OK button to end dialog.  Check modified data
  309.        and send to AutoCAD. */
  310.     set_dimvars(cpkt->dialog);
  311.     ads_done_positioned_dialog(cpkt->dialog, 1, &dimx, &dimy);
  312. }
  313.  
  314.  
  315. /* Show current values in dialog */
  316.  
  317. void
  318. /*FCN*/get_dimvars(hdlg)
  319.   ads_hdlg hdlg;
  320. {
  321.     char *bitem;
  322.     char *ritem;
  323.     char **blist = dimbools;
  324.     char **rlist = dimreals;
  325.     char value[80];
  326.     struct resbuf rb;
  327.  
  328.     for ( ; (bitem = *blist) != NULL; ++blist) {
  329.     ads_getvar(bitem, &rb);
  330.     if (rb.restype != RTSHORT) {
  331.             ads_printf(/*MSG17*/"No such AutoCAD variable: %s\n", bitem);
  332.         if (rb.restype == RTSTR)
  333.         free(rb.resval.rstring);
  334.         continue;
  335.     }
  336.         sprintf(value, "%d", rb.resval.rint);
  337.     ads_set_tile(hdlg, bitem, value);
  338.     }
  339.     for ( ; (ritem = *rlist) != NULL; ++rlist) {
  340.     ads_getvar(ritem, &rb);
  341.     if (rb.restype != RTREAL) {
  342.             ads_printf(/*MSG18*/"No such AutoCAD variable: %s\n", ritem);
  343.         if (rb.restype == RTSTR)
  344.         free(rb.resval.rstring);
  345.         continue;
  346.     }
  347.     ads_rtos(rb.resval.rreal, -1, -1, value);
  348.     ads_set_tile(hdlg, ritem, value);
  349.     }
  350.     ads_set_tile(hdlg, /*MSG0*/"test_item", /*MSG0*/"test_value");
  351. }
  352.  
  353.  
  354. /* set modified dimension variables in AutoCAD */
  355.  
  356. static void
  357. /*FCN*/set_dimvars(hdlg)
  358.   ads_hdlg hdlg;
  359. {
  360.     char *bitem;
  361.     char *ritem;
  362.     char **blist = dimbools;
  363.     char **rlist = dimreals;
  364.     char val[MAX_TILE_STR+1];
  365.     struct resbuf rb;
  366.  
  367.     /* Check all the checkbox tiles for new values */
  368.     for ( ; (bitem = *blist) != NULL; ++blist) {
  369.     /* Get the new value of tiles */
  370.     ads_get_tile(hdlg, bitem, val, MAX_TILE_STR);
  371.  
  372.     rb.restype = RTSHORT;
  373.     rb.resval.rint = atoi(val);
  374.     ads_setvar(bitem, &rb);
  375.     }
  376.     /* Check all the edit box tiles for new values */
  377.     for ( ; (ritem = *rlist) != NULL; ++rlist) {
  378.     /* Get the new value of tiles */
  379.     ads_get_tile(hdlg, ritem, val, MAX_TILE_STR);
  380.     rb.restype = RTREAL;
  381.     ads_distof(val, -1, &rb.resval.rreal);
  382.     ads_setvar(ritem, &rb);
  383.     }
  384.     ads_get_tile(hdlg, /*MSG0*/"test_item", /*MSG0*/"test_value", 50);
  385. }
  386.  
  387.  
  388.  
  389. /* 
  390.  
  391.         SETCOLOR -- Color Dialog
  392.  
  393. */
  394.  
  395. int
  396. /*FCN*/setcolor()
  397. {
  398.     int idx;
  399.     ads_hdlg hdlg;
  400.     int dlg_status;
  401.     struct resbuf rb;
  402.     char *cptr, *ptr;
  403.     short colorsave;
  404.     char cname[10];
  405.     int dcl_id;
  406.  
  407.     /* Load the dialog file */
  408.     ads_load_dialog(/*MSG0*/"dlgtest.dcl", &dcl_id);
  409.     if (dcl_id < 0) {
  410.         ads_printf(/*MSG19*/"Error loading \"dlgtest.dcl\"\n");
  411.     return -1;
  412.     }
  413.     /* initialize the setcolor dialog, no default callback function */
  414.     ads_new_dialog(/*MSG0*/"setcolor", dcl_id, (CLIENTFUNC)0, &hdlg);
  415.     if (hdlg == NULL) {
  416.         ads_printf(/*MSG20*/"new_dialog for setcolor failed\n");
  417.     ads_unload_dialog(dcl_id);
  418.     return -1;
  419.     }
  420.     /* Get the current color from AutoCAD */
  421.     ads_getvar(/*MSG0*/"CECOLOR", &rb);
  422.     /* AutoCAD  currently returns  "human readable" colour strings
  423.        like "1 (red)" for the standard colours.  Trim  the  string
  424.        at  the    first space to guarantee we have a valid string to
  425.        restore the colour later.  */
  426.     cptr = rb.resval.rstring;
  427.     ptr = strchr(cptr, ' ');
  428.     if (ptr != NULL)
  429.     *ptr = EOS;
  430.     strcpy(colorstr, cptr);
  431.     color = colorsave = atoi(colorstr);
  432.  
  433.     /* Update other tiles when one is changed by using callback
  434.        functions */
  435.     ads_action_tile(hdlg, /*MSG0*/"edit_col", (CLIENTFUNC)editcol_cb);
  436.     ads_action_tile(hdlg, /*MSG0*/"list_col", (CLIENTFUNC)listcol_cb);
  437.  
  438.     ads_action_tile(hdlg, /*MSG0*/"help", (CLIENTFUNC)help_cb);
  439.  
  440.     /* Use the client data pointer to store the key of each tile,
  441.        for convenient access during callbacks.     We could use
  442.        get_attr_string during the callbacks instead. */
  443.     ads_client_data_tile(hdlg, /*MSG0*/"edit_col", /*MSG0*/"edit_col");
  444.     ads_client_data_tile(hdlg, /*MSG0*/"list_col", /*MSG0*/"list_col");
  445.  
  446.     /* Fill list box */
  447.     ads_start_list(hdlg, /*MSG0*/"list_col", LIST_NEW, 0);
  448.     for (idx = 0; idx < 8; ++idx)
  449.     ads_add_list(colorlist[idx]);
  450.     for (idx = 8; idx < 256; ++idx) {
  451.         sprintf(cname, "%d", idx);
  452.     ads_add_list(cname);
  453.     }
  454.     ads_end_list();
  455.  
  456.     /* Show initial values in edit box, list box, and image tile */
  457.     ads_set_tile(hdlg, /*MSG0*/"edit_col", colorstr);
  458.     ads_set_tile(hdlg, /*MSG0*/"list_col", colorstr);
  459.     dlg_colortile(hdlg, /*MSG0*/"show_image", color, TRUE);
  460.  
  461.     /* Hand control over to the dialog until OK or CANCEL is pressed */
  462.     ads_start_dialog(hdlg, &dlg_status);
  463.  
  464.     /* Dialog ended with OK button, "accept"? */
  465.     if (dlg_status == DLGOK) {
  466.     if (color == 0)
  467.             ads_command(RTSTR, /*MSG0*/"_.COLOUR", RTSTR, /*MSG0*/"_BYLAYER",
  468.             RTNONE);
  469.     else
  470.             ads_command(RTSTR, /*MSG0*/"_.COLOUR", RTSHORT, color, RTNONE);
  471.     }
  472.     /* free all memory for dialog */
  473.     ads_unload_dialog(dcl_id);
  474.  
  475.     return (dlg_status == DLGOK) ? color : colorsave;
  476. }
  477.  
  478.  
  479. /* EDITCOL_CB -- ADS callback for color edit box.  */
  480.  
  481. static void CALLB
  482. /*FCN*/editcol_cb(cpkt)
  483.   ads_callback_packet *cpkt;
  484. {
  485.     if (cpkt->value == NULL || cpkt->value[0] == EOS)
  486.     return;
  487.  
  488.     ads_set_tile(cpkt->dialog, /*MSG0*/"list_col", cpkt->value);
  489.     color = atoi(cpkt->value);
  490.     dlg_colortile(cpkt->dialog, /*MSG0*/"show_image", color, TRUE);
  491. }
  492.  
  493. #define MAXKEYLEN 32
  494.  
  495. /* LISTCOL_CB -- ADS callback for color edit box.  */
  496.  
  497. static void CALLB
  498. /*FCN*/listcol_cb(cpkt)
  499.   ads_callback_packet *cpkt;
  500. {
  501.     char akey[MAXKEYLEN];
  502.  
  503.     if (cpkt->value == NULL || cpkt->value[0] == EOS)
  504.     return;
  505.  
  506.     /* Get key */
  507.     ads_get_attr_string(cpkt->tile, /*MSG0*/"key", akey, MAXKEYLEN);
  508.  
  509.     ads_set_tile(cpkt->dialog, /*MSG0*/"edit_col", cpkt->value);
  510.     color = atoi(cpkt->value);
  511.     dlg_colortile(cpkt->dialog, /*MSG0*/"show_image", color, TRUE);
  512. }
  513.  
  514. /*HELP_CB -- ADS callback for help button.  */
  515.  
  516. static void CALLB
  517. /*FCN*/help_cb(cpkt)
  518.   ads_callback_packet *cpkt;
  519. {
  520.     /* request main Acad help, just to show you can do it. */
  521.     ads_help(NULL, NULL, 0);
  522. }
  523.  
  524.  
  525.  
  526.  
  527.  
  528. /* DLG_COLORTILE -- Color a tile.  "dialog" can be NULL to use the
  529.                     current dialog.  Values for "color" may be 0 to
  530.                     255.  "color" may be one of the defines in
  531.             colours.h, such as RED.   Draws border if
  532.             borderflag is TRUE. */
  533.  
  534. void
  535. /*FCN*/dlg_colortile(hdlg, key, color, borderflag)
  536.   ads_hdlg hdlg;
  537.   char *key;
  538.   short color;
  539.   int borderflag;
  540. {
  541.     short width, height;
  542.  
  543.     ads_dimensions_tile(hdlg, key, &width, &height);
  544.     ads_start_image(hdlg, key);
  545.     ads_fill_image(0, 0, width, height, color);
  546.  
  547.     if (borderflag) {
  548.     /* Put border around color */
  549.     dlg_rect(0, 0, width, height, WHITE);
  550.     }
  551.     ads_end_image();
  552. }
  553.  
  554.  
  555.  
  556. /* DLG_RECT -- Draw a rectangle in an image tile.  Use tile dimensions
  557.            to draw border around tile.  Assumes start_image
  558.            has been called. */
  559.  
  560. void
  561. /*FCN*/dlg_rect(x, y, width, height, color)
  562.   short x;
  563.   short y;
  564.   short width;
  565.   short height;
  566.   short color;
  567. {
  568.     short x2, y2;
  569.  
  570.     x2 = x + width - 1;
  571.     y2 = y + height - 1;
  572.     ads_vector_image(x,  y,  x,  y2, color);
  573.     ads_vector_image(x,  y2, x2, y2, color);
  574.     ads_vector_image(x2, y2, x2, y,  color);
  575.     ads_vector_image(x2, y,  x,  y,  color);
  576. }
  577.  
  578.  
  579.